package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.atguigu.jxc.util.StringUtil;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;

    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @Override
    public List<Supplier> getPage(Integer page, Integer rows, String supplierName) {
        // 计算start 起始行
        Integer start = (page - 1) * rows;
        List<Supplier> list = supplierDao.getPage(start,rows,supplierName);
        return list;
    }

    /**
     * 修改或插入供应商
     * @param supplierId
     * @param supplier
     */
    @Override
    public void update(Long supplierId, Supplier supplier) {
        if(supplierId==null){
            supplierDao.insert(supplier);
        }else{
            supplierDao.update(supplierId,supplier);
        }
    }

    @Override
    public void delete(String ids) {
        if(StringUtil.isNotEmpty(ids)){
            String[] split = ids.split(",");
            for (String id : split) {
                if(null != id && id != "") {
                    supplierDao.delete(Integer.parseInt(id));
                }
            }
        }
    }

    @Override
    public List<Supplier> getComboboxList(String q) {
        return supplierDao.getComboboxList(q);
    }
}
